  import Metronic from '../../global/js/metronic.js'
var Calendar = function() {


    return {
        //main function to initiate the module
        init: function() {
            Calendar.initCalendar();
        },

        initCalendar: function() {

            if (!jQuery().fullCalendar) {
                return;
            }

            var date = new Date();
            var d = date.getDate();
            var m = date.getMonth();
            var y = date.getFullYear();

            var h = {};

            if (Metronic.isRTL()) {
                if ($('#calendar').parents(".portlet").width() <= 720) {
                    $('#calendar').addClass("mobile");
                    h = {
                        right: 'title, prev, next',
                        center: '',
                        left: 'agendaDay, agendaWeek, month, today'
                    };
                } else {
                    $('#calendar').removeClass("mobile");
                    h = {
                        right: 'title',
                        center: '',
                        left: 'agendaDay, agendaWeek, month, today, prev,next'
                    };
                }
            } else {
                if ($('#calendar').parents(".portlet").width() <= 720) {
                    $('#calendar').addClass("mobile");
                    h = {
                        left: 'title, prev, next',
                        center: '',
                        right: 'today,month,agendaWeek,agendaDay'
                    };
                } else {
                    $('#calendar').removeClass("mobile");
                    h = {
                        left: 'title',
                        center: '',
                        right: 'prev,next,today,month,agendaWeek,agendaDay'
                    };
                }
            }

            var initDrag = function(el,day) {
                // create an Event Object (http://arshaw.com/fullcalendar/docs/event_data/Event_Object/)
                // it doesn't need to have a start or end
                var eventObject = {
                    title: $.trim(el.text()), // use the element's text as the event title
                };
                // store the Event Object in the DOM element so we can get to it later
                el.data('eventObject', eventObject);

                // make the event draggable using jQuery UI

                el.draggable({
                    zIndex: 999,
                    revert: true, // will cause the event to go back to its
                    revertDuration: 0, //  original position after the drag
                    stop:function(event,ui){ 
                        
                    } 
                });
            };

            var addEvent = function(title,day) {
                //title = title.length === 0 ? "Untitled Event" : title;
                    if(day){
                        var html = $('<div class="external-event label label-default" data-start='+day[0]+' data-end='+day[1]+'><img class="clear" src="../../../static/images/clear.png"/>' + title + '</div>');
                    }else{
                        var html = $('<div class="external-event label label-default"><img class="clear" src="../../../static/images/clear.png"/>' + title + '</div>');
                    }
                    
                    jQuery('#event_box').append(html);
                    initDrag(html,day);
                    $(".external-event .clear").on('click',function(){
                        $(this).closest(".external-event").remove();
                    })
                
                
            };

            $('#external-events div.external-event').each(function() {
                initDrag($(this));
            });

            $('#event_add').unbind('click').click(function() {
                var title = $('#event_title').val();
                

                if(!$(".input-daterange .form-control").val()){
                
                    if(!$("#error").get(0)){
                        var errorHtml = "<span style='color:#ff3300;font-weight:500;' id='error'>错误提示</span>";
                        $("#time").append(errorHtml); 
                    }
                   
                    return;
                }
                 if(title.length == 0){
                      $("#event_title").addClass('error').attr('placeholder','不能为空');
                      return;
                }

                else{
                    if($("#error").get(0)){
                        $("#error").remove();
                    }
                    var value1 = $(".input-daterange .form-control1").val();
                    var value2 = $(".input-daterange .form-control2").val();
                    var option = [value1,value2]                   
                    addEvent(title,option);
                }

            });

            //predefined events
            $('#event_box').html("");
            addEvent("事件1",["2017-9-1","2017-9-2"]);
         

            $('#calendar').fullCalendar('destroy'); // destroy the calendar
            $('#calendar').fullCalendar({ //re-initialize the calendar
                header: h,
                defaultView: 'month', // change default view with available options from http://arshaw.com/fullcalendar/docs/views/Available_Views/ 
                slotMinutes: 15,
                buttonText: {
                  today: '今天',
                  month: '月',
                  week: '周',
                  day: '日'
                },
                columnFormat: {
                      month: 'dddd',
                      week: 'dddd M-d',
                      day: 'dddd M-d'
                },
                titleFormat: {
                      month: 'yyyy年 MMMM月',
                      week: "[yyyy年] MMMM月d日 { '&#8212;' [yyyy年] MMMM月d日}",
                      day: 'yyyy年 MMMM月d日 dddd'
                },
                monthNames: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"],
                editable: true,
                droppable: true, // this allows things to be dropped onto the calendar !!!
                drop: function(date, allDay) { // this function is called when something is dropped
                    // retrieve the dropped element's stored Event Object
                    var originalEventObject = $(this).data('eventObject');
                    // we need to copy it, so that multiple events don't have a reference to the same object
                    var copiedEventObject = $.extend({type:true}, originalEventObject);
                    $(this).remove();
                    // assign it the date that was reported
                    copiedEventObject.allDay = allDay;
                    copiedEventObject.start = $(this).data("start");
                    copiedEventObject.end = $(this).data("end");
                    copiedEventObject.className = $(this).attr("data-class");
                    // render the event on the calendar
                    // the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/)
                    $('#calendar').fullCalendar('renderEvent', copiedEventObject, true);

                    // is the "remove after drop" checkbox checked?
                    if ($('#drop-remove').is(':checked')) {
                        // if so, remove the element from the "Draggable Events" list
                        $(this).remove();
                    }
                },
                events: [
                {
                    title: '所有事件1',
                    start: new Date(y, m, d),
                    backgroundColor: Metronic.getBrandColor('yellow')
                }, {
                    title: '所有事件2',
                    start: '2017-09-1',
                    end: '2017-10-1',
                    backgroundColor: Metronic.getBrandColor('green')
                }, {
                    title: '所有事件3',
                    start: new Date(y, m, d - 3, 16, 0),
                    allDay: false,
                    backgroundColor: Metronic.getBrandColor('red')
                }, {
                    title: '所有事件4',
                    start: new Date(y, m, d + 4, 16, 0),
                    allDay: false,
                    backgroundColor: Metronic.getBrandColor('green')
                },
                //  {
                //     title: '所有事件5',
                //     start: new Date(y, m, d, 10, 30),
                //     allDay: false,
                // }
                ]
            });

            /*删除日程*/
            $("#calendar").on("click",".clear",function(){
                var event =$(this).closest(".fc-event");

                if(event.prev().find(".fc-event-title").text()==event.next().find(".fc-event-title").text()){
                    if(event.next().next().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                        if(event.next().next().next().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                            if(event.next().next().next().next().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                                if(event.next().next().next().next().next().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                                    event.next().next().next().next().next().remove();
                                } 
                                event.next().next().next().next().remove();
                            } 
                            event.next().next().next().remove();
                        } 
                        event.next().next().remove();
                    } 
                     if(event.prev().prev().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                         if(event.prev().prev().prev().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                             if(event.prev().prev().prev().prev().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                                
                                if(event.prev().prev().prev().prev().prev().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                                    event.prev().prev().prev().prev().prev().remove();
                                }
                                event.prev().prev().prev().prev().remove();
                            }
                            event.prev().prev().prev().remove();
                        }
                        event.prev().prev().remove();
                    } 
                    event.next().remove();
                    event.prev().remove();
                    event.remove();
                    return false;
                }

                if(event.prev().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                    if(event.prev().prev().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                        if(event.prev().prev().prev().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                            if(event.prev().prev().prev().prev().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                                if(event.prev().prev().prev().prev().prev().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                                    event.prev().prev().prev().prev().prev().remove();

                                } 
                                event.prev().prev().prev().prev().remove();
                            } 
                            event.prev().prev().prev().remove();
                        } 
                        event.prev().prev().remove();
                    } 

                    event.prev().remove();
                    event.remove();
                    return false;
                }

                if(event.next().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                    if(event.next().next().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                        if(event.next().next().next().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                            if(event.next().next().next().next().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                                if(event.next().next().next().next().next().find(".fc-event-title").text()==event.find(".fc-event-title").text()){
                                    event.next().next().next().next().next().remove();
                                }
                                event.next().next().next().next().remove();
                            }
                            event.next().next().next().remove();
                        } 
                        event.next().next().remove();
                    } 
                    event.next().remove();
                    event.remove();
                    return false;
                }   

                


                $(this).closest(".fc-event").remove();
                
                
                
            })
        }

    };

}();
export default Calendar;